package com.example.xinhuazidian.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.R
import com.example.xinhuazidian.bean.Dizigui

class DiziguiAdapter(
    private val list: MutableList<Dizigui.ContentBean>,
    private var fileName: String
) :
    RecyclerView.Adapter<DiziguiAdapter.MyViewHolder>() {

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvChapter: TextView = view.findViewById(R.id.tvChapter)
        val rv: RecyclerView = view.findViewById(R.id.rv)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_dizigui, parent, false)
        return MyViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val list = list[position]
        with(holder) {
            tvChapter.text = list.chapter

            val staggeredGridLayoutManager =
                StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL)
            rv.layoutManager = staggeredGridLayoutManager
            //加载适配器
            val szjAdapter = if ("lunyu.json" == fileName) {
                LunyuAdapter(list.paragraphs as MutableList<String>)
            } else {
                SZJAdapter(list.paragraphs as MutableList<String>)
            }
            rv.adapter = szjAdapter
        }
    }

    override fun getItemCount() = list.size
}

